package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    public static HttpSession getSession() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    /**
     * 获取当前登录用户
     * @return
     */
    public static Employee getCurrenUser() {
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    /**
     * 设置用户到session中
     * @param employee
     */
    public static void setCurrenUser(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    /**
     * 获取当前权限数据
     * @return
     */
    public static List<String> getPermissionExpression() {
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }

    /**
     * 设置权限到session中
     * @param permissionExpressions
     */
    public static void setPermissionExpression(List<String> permissionExpressions) {
        getSession().setAttribute(PERMISSION_IN_SESSION,permissionExpressions);
    }
}
